package com.situ.mvc.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.SqlSession;

import  com.situ.mvc.dao.IStudentDao;
import  com.situ.mvc.dao.impl.StudentDaoJdbcImpl;
import  com.situ.mvc.dao.impl.StudentDaoMemoryImpl;
import  com.situ.mvc.entity.PageBean;
import  com.situ.mvc.entity.Student;
import  com.situ.mvc.service.IStudentService;
import com.situ.mvc.util.MyBatisUtil;
import com.situ.mvc.vo.StudentSearchCondition;

public class StudentServiceImpl implements IStudentService{
	//private IStudentDao studentDao = new StudentDaoMemoryImpl();
	
	private IStudentDao studentDao = null;
	
	public StudentServiceImpl() {
		 SqlSession sqlSession = MyBatisUtil.getSqlSession();
		 studentDao = sqlSession.getMapper(IStudentDao.class); 
	}

	@Override
	public boolean insert(Student student) {
		int count = studentDao.insert(student);
		return count == 1 ? true : false;
	}

	@Override
	public List<Student> list() {
		List<Student> list = studentDao.list();
		List<Student> copyList = new ArrayList<>();
		for (Student student : list) {
			//student.setName("Java1807-" + student.getName());
			copyList.add(student);
		}
		return copyList;
		//return studentDao.list();
	}

	@Override
	public boolean deleteById(Integer id) {
		int count = studentDao.deleteById(id);
		return count == 1 ? true : false;
	}

	@Override
	public Student findById(Integer id) {
		return studentDao.findById(id);
	}

	@Override
	public boolean update(Student student) {
		int count = studentDao.update(student);
		return count == 1 ? true : false;
	}

	@Override
	public PageBean getPageBean(int pageNo, int pageSize) {
		PageBean pageBean = new PageBean();
		// 当前是第几页
		pageBean.setPageNo(pageNo);
		// 一页有多少条数据
		pageBean.setPageSize(pageSize);
		// 总记录数
		int totalCount = studentDao.getTotalCount();
		pageBean.setTotalCount(totalCount);
		//一共有多少页
		//总数量   每页数量    总页数
		// 10     3      4       10/3=3...1
		// 11     3      4       10/3=3...2
		// 12     3      4       10/3=4
		// 13     3      5       10/3=4...1
		int totalPage = (int)Math.ceil((double)totalCount / pageSize);
		pageBean.setTotalPage(totalPage);
		// 当前页的数据
		int offset = (pageNo - 1) * pageSize;
		List<Map<String, Object>> list = studentDao.pageList(offset, pageSize);
		pageBean.setList(list);
		
		return pageBean;
	}

	@Override
	public boolean deleteAll(String[] selectIds) {
		//有缺点：每次打开连接，关闭连接，非常消耗性能
		/*for (String id : selectIds) {
			studentDao.deleteById(Integer.parseInt(id));
		}*/
		
		//批量删除
		int count = studentDao.deleteAll(selectIds);
		return count == selectIds.length ? true : false;
	}
	
	public static void main(String[] args) {
		System.out.println(3/2);
		double d1 = 3/2;
		System.out.println(d1);
		int totalCount=14;
		int pageSize=3;
		System.out.println(totalCount / pageSize);
		double d = totalCount / pageSize;
		System.out.println("d: " + d);
		int totalPage = (int)Math.ceil(totalCount / pageSize);
		System.out.println(totalPage);
	}

	@Override
	public boolean checkName(String name) {
		int count = studentDao.selectCountByName(name);
		return count > 0 ? true : false;
	}

	@Override
	public PageBean<Student> getPageBean(StudentSearchCondition searchCondition) {
		PageBean pageBean = new PageBean();
		// 当前是第几页
		pageBean.setPageNo(searchCondition.getPageNo());
		// 一页有多少条数据
		pageBean.setPageSize(searchCondition.getPageSize());
		// 总记录数 private Integer totalCount
		// select count(*) from student;
		// select count(*) from student where name like "%张%" and age=20;
		int totalCount = studentDao.getTotalCount(searchCondition);
		pageBean.setTotalCount(totalCount);
		//一共有多少页
		//总数量   每页数量    总页数
		// 10     3      4       10/3=3...1
		// 11     3      4       10/3=3...2
		// 12     3      4       10/3=4
		// 13     3      5       10/3=4...1
		int totalPage = (int)Math.ceil((double)totalCount / searchCondition.getPageSize());
		pageBean.setTotalPage(totalPage);
		// 当前页的数据
		List<Map<String, Object>> list = studentDao.pageList(searchCondition);
		pageBean.setList(list);
		
		return pageBean;
	}

}
